package com.mentalhealthplatform.global.rbac.controller;

import com.mentalhealthplatform.global.rbac.dto.normal.MenuDTO;
import com.mentalhealthplatform.global.rbac.dto.normal.MenuHierarchyDTO;
import com.mentalhealthplatform.global.rbac.service.entity.MenuService;
import com.mentalhealthplatform.global.rbac.service.relation.MenuPermissionService;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Set;

@RestController
@RequestMapping("/api/menus")
public class MenuController {

    @Autowired
    private MenuPermissionService menuPermissionService;

    @Autowired
    private MenuService menuService;

    @GetMapping("/hierarchy")
    public Set<MenuHierarchyDTO> getMenuHierarchy() {
        return menuPermissionService.getMenuHierarchy();
    }

    @GetMapping
    public Set<MenuDTO> getAllMenus() {
        return menuService.getMenuHierarchy();
    }

    // 新增菜单
    @PostMapping
    public ResponseEntity<MenuDTO> createMenu(@RequestBody @Valid MenuDTO menuDTO) {
        MenuDTO createdMenu = menuService.createMenu(menuDTO);
        return ResponseEntity.ok(createdMenu);
    }

    // 更新菜单
    @PutMapping("/{id}")
    public ResponseEntity<MenuDTO> updateMenu(@PathVariable Long id, @RequestBody @Valid MenuDTO menuDTO) {
        MenuDTO updatedMenu = menuService.updateMenu(id, menuDTO);
        return ResponseEntity.ok(updatedMenu);
    }

    // 删除菜单
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteMenu(@PathVariable Long id) {
        menuService.deleteMenu(id);
        return ResponseEntity.noContent().build();
    }

    // 切换菜单状态
    @PatchMapping("/{id}/status")
    public ResponseEntity<MenuDTO> toggleMenuStatus(@PathVariable Long id) {
        MenuDTO toggledMenu = menuService.toggleMenuStatus(id);
        return ResponseEntity.ok(toggledMenu);
    }

    // 切换菜单可视状态
    @PatchMapping("/{id}/visibility")
    public ResponseEntity<MenuDTO> toggleMenuVisibility(@PathVariable Long id) {
        MenuDTO toggledMenu = menuService.toggleMenuVisibility(id);
        return ResponseEntity.ok(toggledMenu);
    }



}
